package main

import (
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"fmt"
	"gorm_demo/model"
)

func main() {

	db, err := gorm.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8&parseTime=True&loc=Local")
	db.LogMode(true)
	if err != nil {
		fmt.Println(err.Error())
	}
	if ok := db.HasTable(&model.User{}); !ok {
		db.CreateTable(&model.User{})
		db.Model(&model.User{}).AddUniqueIndex("index_user_name", "user_name")
	}
	if ok := db.HasTable(&model.Profile{}); !ok {
		db.CreateTable(&model.Profile{})
	}
	if ok := db.HasTable(&model.Toy{}); !ok {
		db.CreateTable(&model.Toy{})
	}
	if ok := db.HasTable(&model.Student{}); !ok {
		db.CreateTable(&model.Student{})
	}
	if ok := db.HasTable(&model.Persion{}); !ok {
		db.CreateTable(&model.Persion{})
	}
	if ok := db.HasTable(&model.Email{}); !ok {
		db.CreateTable(&model.Email{})
	}
	if ok := db.HasTable(&model.Animal{}); !ok {
		db.CreateTable(&model.Animal{})
	}
	if ok := db.HasTable(&model.Food{}); !ok {
		db.CreateTable(&model.Food{})
	}

	defer db.Close()
}
